﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PictureManager.Core.Provider;
using UtilLib;
using System.Xml.Serialization;
using Infrastructure.Library;

namespace PictureManager.Core
{
    public partial class PictureItem : BusinessBase<PictureItem, String>, IComparable<PictureItem>
    {
        #region Constants and Fields
        /// <summary>
        /// The sync root.
        /// </summary>
        private static readonly object lockObj = new object();

        private String parentID;
        private String path;
        private String name;
        private String desc;
        private Boolean isNameBeFileName = true;
        private Dictionary<String, String> fileNames = new Dictionary<string, string>();
        #endregion

        #region Properties

        public String ParentID
        {
            get { return parentID; }
            set { base.SetValue("ParentID", value, ref parentID); }
        }

        public String Path
        {
            get { return path; }
            set { base.SetValue("Path", value, ref path); }
        }

        public String Name
        {
            get { return name; }
            set { base.SetValue("Name", value, ref name); }
        }

        public String Desc
        {
            get { return desc; }
            set { base.SetValue("Desc", value, ref desc); }
        }

        public bool IsNameBeFileName
        {
            get { return isNameBeFileName; }
            set { base.SetValue("IsNameBeFileName", value, ref isNameBeFileName); }
        }

        public Dictionary<String, String> FileNames
        {
            get { return fileNames; }
            set { base.SetValue("FileNames", value, ref fileNames); }
        }

        private String relativePath;
        public String RelativePath
        {
            get
            {
                if (UtilLib.StringUtil.IsNullOrWhitespace(relativePath))
                {
                    StringBuilder relativePathBuilder = new StringBuilder();
                    PictureItem item = this;
                    while (item.ParentID != PictureSettings.Instance.PictureRootNode)
                    {
                        relativePathBuilder.Insert(0, @"\" + item.Name);
                        item = GetItem(item.ParentID);
                    }
                    relativePathBuilder.Insert(0, item.Name);
                    relativePath = relativePathBuilder.ToString();
                }
                return relativePath;

            }
            internal set
            {
                relativePath = value;
            }
        }
        /// <summary>
        /// The blog rolls.
        /// </summary>
        private static List<PictureItem> pictureList;
        /// <summary>
        ///     Gets all of the BlogRollItems from the data store.
        /// </summary>
        public static List<PictureItem> PictureList
        {
            get
            {
                if (pictureList == null)
                {
                    lock (lockObj)
                    {
                        if (pictureList == null)
                        {
                            pictureList = PictureService.GetPictures();
                        }
                    }
                }

                return pictureList;
            }
        }
        private static PictureItem rootItem;
        public static PictureItem RootItem
        {
            get
            {
                if (rootItem == null)
                {
                    rootItem = new PictureItem();
                    rootItem.ID = PictureSettings.Instance.PictureRootNode;
                    rootItem.Name = PictureSettings.Instance.PictureRootNode;
                    rootItem.ParentID = PictureSettings.Instance.PictureRootNode;
                }
                return rootItem;
            }
        }

        #endregion

        protected override void DataDelete()
        {
            if (this.Deleted)
            {
                PictureService.DeletePictureItem(this);
            }
            this.Dispose();
        }

        protected override void DataInsert()
        {
            if (this.New)
            {
                PictureService.InsertPictureItem(this);
            }
        }

        protected override PictureItem DataSelect(String id)
        {
            return PictureService.GetPictureItem(id);
        }

        protected override void DataUpdate()
        {
            if (this.IsChanged)
            {
                PictureService.UpdatePictureItem(this);
            }
        }

        protected override void ValidationRules()
        {
            this.AddRule("Name", "Name must be set", StringUtil.IsNullOrWhitespace(this.Name));
        }

        public static void SaveAll()
        {
            PictureService.SavePictureList();
        }

        public Boolean IsParentID(String parentId)
        {
            PictureItem item = this;
            if (parentId == PictureSettings.Instance.PictureRootNode) return true;
            while (item.ParentID != null && item.ParentID != PictureSettings.Instance.PictureRootNode)
            {
                if (item.ParentID == parentId) return true;
                item = GetItem(item.ParentID);
                if (item == null) break;
            }
            return false;
        }

        public void SetRelativePathEmpty()
        {
            this.RelativePath = String.Empty;

            PictureList.ForEach(p =>
                {
                    if (p.ParentID == this.ID && p.ID != this.ID)
                        p.SetRelativePathEmpty();
                });

        }

        #region IComparable<PictureItem> Members

        public int CompareTo(PictureItem other)
        {
            return this.relativePath.CompareTo(other.relativePath);
        }
        #endregion
    }
}
